package com.ruoyi.web.controller.systembasic;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysPostService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.systembasic.domain.SmesEmployee;
import com.ruoyi.mes.systembasic.service.ISmesEmployeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工管理Controller
 * 
 * @author yanqin
 * @date 2020-11-24
 */
@RestController
@RequestMapping("/systembasic/employee")
public class SmesEmployeeController extends BaseController
{
    @Autowired
    private ISmesEmployeeService smesEmployeeService;

    @Autowired
    private ISysPostService postService;

    /**
     * 查询员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmesEmployee smesEmployee)
    {
        startPage();
        List<SmesEmployee> list = smesEmployeeService.selectSmesEmployeeList(smesEmployee);
        return getDataTable(list);
    }

    /**
     * 导出员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:export')")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesEmployee smesEmployee)
    {
        List<SmesEmployee> list = smesEmployeeService.selectSmesEmployeeList(smesEmployee);
        ExcelUtil<SmesEmployee> util = new ExcelUtil<SmesEmployee>(SmesEmployee.class);
        return util.exportExcel(list, "employee");
    }

    /**
     * 获取员工管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:query')")
    @GetMapping(value = "/{employeeNo}")
    public AjaxResult getInfo(@PathVariable("employeeNo") String employeeNo)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(employeeNo))
        {
            ajax.put(AjaxResult.DATA_TAG, smesEmployeeService.selectSmesEmployeeById(employeeNo));
            ajax.put("postIds", postService.selectPostListByEmployeeId(employeeNo));
        }
        return ajax;
    }

    /**
     * 新增员工管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesEmployee smesEmployee)
    {
        smesEmployee.setCreateBy(SecurityUtils.getUsername());
        return toAjax(smesEmployeeService.insertSmesEmployee(smesEmployee));
    }

    /**
     * 修改员工管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesEmployee smesEmployee)
    {
        smesEmployee.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(smesEmployeeService.updateSmesEmployee(smesEmployee));
    }

    /**
     * 删除员工管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:employee:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{employeeNos}")
    public AjaxResult remove(@PathVariable String[] employeeNos)
    {
        return toAjax(smesEmployeeService.deleteSmesEmployeeByIds(employeeNos));
    }
}
